import { ResourceType } from "./modules";

const ElectronStore = require('electron-store')
/**
 * 存储
 */
export class Storage<T> {

    private storeHandler;

    getHandler() {
        if (!this.storeHandler) {
            this.storeHandler  = new ElectronStore();
        }
        return this.storeHandler;
    }

    /**
     * get from store with default value ?
     * @param key 
     */
    storageGet(key: string, defaultValue: any): T{
        return JSON.parse(this.getHandler().get(key, defaultValue));
    }

    /**
     * set to store.
     * @param key 
     * @param t 
     */
    storageSet(key: string, t: T) {
        if (t) {
            let tStr = JSON.stringify(t);
            this.getHandler().set(key, tStr);
        }
    }
}